/**
 * 运维管理service
 */
(function(window, angular, undefined){
	/**
	 * 运维管理service
	 */
	var OperatorManagerService = angular.module('OperatorManagerService', ['ui.router','itfreer','waterCommon']);
	
	OperatorManagerService.factory('$TaskNavService', function($state,$wdp,$formObject,$rootScope,$itfreerHttp,$httpData,$queryObj,$viewData,$navLocation,$http,$compile) {
		$NavService = {
			BackToTask : function(){
				$("#taskQueryMain").show();
				$("#taskDetail").hide();
				 var stateName = $state.current.name;
				 stateName = stateName.substring(0,stateName.lastIndexOf("."))+".blank";
				 $state.go(stateName);
			}
		};
		
		return $NavService;
	});
	
	OperatorManagerService.factory('OperationStationJobService',['$http','$q',function($http,$q){
		var services = {};
		
		services.postQuery=function(url,queryObj,pageSize,pageIndex){
			queryObj.pageSizeData = pageSize;
			queryObj.pageIndexData = pageIndex;
			var defered = $q.defer();
			$http({
				method:"POST",
				url:getBasePath()+url,
				data:queryObj
			}).success(function(data){
				defered.resolve(data);
			}).error(function(data){
				defered.reject(data);
			});
			return defered.promise;
		};
		
		services.stationJobQueryObj={};
		
		return services;
	}]);
	
	OperatorManagerService.factory('DateService',function(){
		var services = {};
		services.addDate = function(date,days){ 
			date = date || new Date();
			days = days || 0;
			var d=new Date(date); 
			d.setDate(d.getDate()+ days); 
			var month=d.getMonth()+1; 
			var day = d.getDate(); 
			if(month<10){ 
				month = "0"+month; 
			} 
			if(day<10){ 
				day = "0"+day; 
			} 
			var val = d.getFullYear() + "-" + month + "-" + day; 
			return val; 
		}
		
		services.addDateTime = function(date,days){
			date = date || new Date();
			days = days || 0;
			var d=new Date(date); 
			d.setDate(d.getDate()+days); 
			var month=d.getMonth()+1; 
			var day = d.getDate();
			var hour = d.getHours();
			var min = d.getMinutes();
			var sec = d.getSeconds();
			if(month<10){ 
				month = "0"+month; 
			} 
			if(day<10){ 
				day = "0"+day; 
			} 
			if(hour<10){ 
				hour = "0"+hour; 
			} 
			if(min<10){ 
				min = "0"+min; 
			} 
			if(sec<10){ 
				sec = "0"+sec; 
			} 
			var val = d.getFullYear()+"-"+month+"-"+day+" "+hour+":"+min+":"+sec; 
			return val; 
		}
		
		return services;
	});
	
	OperatorManagerService.factory('UploadService',function(){
		var services = {};
		services.upload = function(onSuccess,extensions,maxSize){
			var downloadUrl = getBasePath()+'/plupload/download.do';
			var filters = extensions ? [{title : "文档", extensions : extensions}]:[{title : "所有文档", extensions : "*"}];
			var maxSize = maxSize ? maxSize : "200";
			var html = '<div></div>';
			var el = document.createElement("div");
			el.innerHTML = html;
			var _winEl = el.firstChild;
			$(_winEl).window({   
				title:'上传文件',
				minimizable:false,
				collapsible:false,
				shadow:true,
				maximizable:false,
				closed:true,
				width:600,    
				height:400,
				modal:true,
				onOpen:init_uploader
			});
			$(_winEl).window('open');
			
			var errors = [];
			
			//初始化上传控件
			function init_uploader () {
				if ($("#uploader")[0]) {
					$("#uploader").remove();
				};
				$(_winEl).append('<div id="uploader"><p>您的浏览器未安装 Flash, '
						+'Gears, BrowserPlus 或者支持 HTML5。</p></div>');
				$("#uploader").pluploadQueue({
					// General settings
					runtimes : 'gears,flash,browserplus,html5,html4',
					url : getBasePath()+'/plupload/upload.do',
					max_file_size : maxSize+'mb',
					unique_names : true,
					multiple_queues : true,
					chunk_size: '512kb',
					// Specify what files to browse for
					filters : filters,
					// Flash settings
					silverlight_xap_url : getBasePath()+'/style/script/plupload/Moxie.xap',
					init:{
						FileUploaded:function(uploader,file,response){
							if(response.response){
								var rs = $.parseJSON(response.response);
								if(rs.status){
									var fileInfo = rs.newName + ":" + file.name;
									if(onSuccess){
										onSuccess({file:fileInfo});	
									}
								}else{
									errors.push(file.name);
								}
							}
						},
						UploadComplete:function(uploader,fs){
							uploader.splice();//从上传队列中删除所有文件
							uploader.refresh();
							uploader.destroy();//ie9以下若不销毁此对象第二次上传会十分缓慢
							$("#uploader").remove();
							$(_winEl).window('close');
						}
					}
				});
			}
		}
		return services;
	});
}(window, angular));